﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using shaco.ActionExtensions;

namespace ShootMan
{
    public class DefaultBulletController : MonoBehaviour, shaco.Base.IObjectPoolData
    {
        [HideInInspector]
        public Vector3 moveDirection = Vector3.zero;

        [SerializeField]
        private float _maxAliveTime = 5.0f;

        [SerializeField]
        private int _addScore = 1;

        private Vector3 _initLocalPos = Vector3.zero;
        private Vector3 _initLocalAngle = Vector3.zero;

        void Awake()
        {
            _initLocalPos = this.transform.localPosition;
            _initLocalAngle = this.transform.localEulerAngles;
        }

        void OnEnable()
        {
            this.transform.localPosition = _initLocalPos;
            this.transform.localEulerAngles = _initLocalAngle;
            this.Delay(() => this.RecyclingWithPool(), _maxAliveTime);
        }

        void Update()
        {
            this.transform.Translate(this.moveDirection);
        }

        void OnDestroy()
        {
            if (shaco.GameHelper.objectpool.IsInstantiated(this))
                this.RecyclingWithPool();
        }

        void OnCollisionEnter(Collision other)
        {
            if (other.gameObject.layer != (int)GlobalDefines.Layers.Enmey)
                return;

            this.RecyclingWithPool();

            if (shaco.GameHelper.objectpool.IsInstantiated(other.gameObject))
                other.gameObject.RecyclingWithPool();

            this.InvokeEvent(new ShootAddScoreEvent() { score = _addScore });
        }

        public void OnRecycle()
        {
            this.StopAllAction();
        }
    }
}